#include <iostream>
using namespace std;
//嵌套类的定义
//Line属于外部类
class Line{
public:
    Line(int x1, int y1, int x2, int y2)
    :_pt1(x1, y1)
    ,_pt2(x2, y2)
    {
        cout << "Line(int,int,int,int)" << endl;
    }
private:
    //Point属于内部类
    class Point{
    public:
        Point(int x,int y)
        :_x(x)
        ,_y(y)
        {
            cout << "Point(int,int)" << endl;
        }
        friend ostream & operator<<(ostream & os, const Line::Point & rhs);

    private:
        int _x;
        int _y;
    };// end of class Point;

    friend ostream & operator<<(ostream & os, const Line & rhs);
    friend ostream & operator<<(ostream & os, const Line::Point & rhs);

private:
    Point _pt1;
    Point _pt2;
    double _height;
};

//将当前函数设置为Line::Point的友元
//如果Line::Point位于私有区域，
//不仅需要将当前函数设置为Point的友元(为了去访问_x和_y)
//还需要设置为Line的友元(为了去访问私有的Line::Point类)
//所以该函数需要设置双友元
ostream & operator<<(ostream & os, const Line::Point & rhs){
    os << rhs._x <<  "," << rhs._y;
    return os;
}

//输出流运算符需要使用友元的形式
ostream & operator<<(ostream & os, const Line & rhs){
    os << rhs._pt1 << "------>" << rhs._pt2;
    return os;
}
//此时，还需要一个os << Point的输出流运算符

void test(){
    //cout << sizeof(Line) << endl;
    //分别创建Line对象和Point对象
    Line line(1, 2, 3, 4);
    //对于嵌套类来说，如果希望单独创建该类对象
    //那么需要使用外部类::内部类的方式来进行创建
    //Point pt1(1, 0);error
    Line::Point pt1(1, 0);//ok
    cout << line << endl;
}

int main()
{
    test();
    return 0;
}


